package userServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.entitybeans.User;
import swim2.sessionbeans.*;

public class UserPanelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public UserPanelServlet() {
        super();
    }

	
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		try{
			Context ctx = getInitialContext();			
			UserSystemRemote userSystemRemote = (UserSystemRemote) ctx.lookup("userJNDI");
			Context ctx2 = getInitialContext();			
			LoginSystemRemote loginSystemRemote = (LoginSystemRemote) ctx2.lookup("swim2JNDI");
			int id = (Integer) request.getAttribute("id");
			String completeName = userSystemRemote.getCompleteName(id);
			ArrayList<Integer> reqId = (ArrayList<Integer>) userSystemRemote.getNewFriendReqId(id);
			ArrayList<User> suggestion = new ArrayList<User>();
			if(reqId.size() > 0) {
				for (int i = 0; i < reqId.size(); i++) {
					ArrayList<User> temp = userSystemRemote.findSuggestion(reqId.get(i), id);
					for (int j = 0; j < temp.size(); j++) {
						suggestion.add(temp.get(j));
					}
				}
				for (int i = 5; i < suggestion.size(); i++) {
					suggestion.remove(i);
					i--;
				}
			}
			ArrayList<Integer> newFriendsId = userSystemRemote.getNewFriendships(id);
			ArrayList<String> newFriendsName = new ArrayList<String>();
			for(int i=0; i<newFriendsId.size(); i++){
				newFriendsName.add(userSystemRemote.getCompleteName(newFriendsId.get(i)));
			}
			String newFriendsNotification = new String();
			for(int j=0; j<newFriendsName.size()-3; j++){
				newFriendsNotification = newFriendsNotification + newFriendsName.get(j) + ", ";
			}
			if(newFriendsName.size()>1){
				newFriendsNotification = newFriendsNotification + newFriendsName.get(newFriendsName.size()-2) + " e " + newFriendsName.get(newFriendsName.size()-1) + " hanno accettato la tua richiesta di amicizia.";
			}
			else if(newFriendsName.size()==1) newFriendsNotification = newFriendsNotification + newFriendsName.get(newFriendsName.size()-1) + " ha accettato la tua richiesta di amicizia.";
			
			int unreadRequests = userSystemRemote.checkUnreadFriendRequests(id);
			

			String mail = userSystemRemote.getMail(id);
			ArrayList<String> abilities = (ArrayList<String>) userSystemRemote.getUserAbilities(id);
			ArrayList<Float> ratings = new ArrayList<Float>();
			for(int i=0; i<abilities.size(); i++){
				ratings.add(userSystemRemote.calculateRating(id, userSystemRemote.getAbilityId(abilities.get(i))));
			}
			ArrayList<String> allAbilities = (ArrayList<String>) loginSystemRemote.getAbilities();
			
			request.setAttribute("completeName", completeName);
			request.setAttribute("newFriendsNotification", newFriendsNotification);
			request.setAttribute("unreadRequests", unreadRequests);
			request.setAttribute("id", id);
			request.setAttribute("mail", mail);
			request.setAttribute("abilities", abilities);
			request.setAttribute("allAbilities", allAbilities);
			request.setAttribute("ratings", ratings);
			request.setAttribute("suggestion", suggestion);
			RequestDispatcher dispatcher =
			request.getRequestDispatcher("/UserJsp/UserPanel.jsp");
			dispatcher.forward(request, response);	
		}
		catch (Exception e) {
			e.printStackTrace();
			out.print("Error: user panel exception");
		}
	}

	static public Context getInitialContext() throws Exception{
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
		return new InitialContext(env);
	}
}
